﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using AnyGrid.Renderers;
using AnyGrid.Metadata;

namespace AnyGrid.Tests.Renderers
{
    [TestClass]
    public class BaseGridRendererTests
    {
        private class KeyAttributeClass
        {
            // don't return this one; [Key] supercedes
            public int ID { get; set; }
            public int Foo { get; set; }
            [Key]
            public int MyKey { get; set; }
            public int Bar { get; set; }
        }

        [TestMethod]
        public void IdColumn_returns_KeyAttribute_column()
        {
            // MVC doesn't support KeyAttribute; AnyGrid does.
            var mm = new ExtendedDataAnnotationsModelMetadataProvider().GetMetadataForType(null, typeof(KeyAttributeClass));
            var expected = "MyKey";

            var actual = BaseGridRenderer.IdColumn(mm);

            Assert.AreEqual(expected, actual);
        }

        private class IdPropertyClass
        {
            public int Foo { get; set; }
            public int ID { get; set; }
            public int Bar { get; set; }
        }

        [TestMethod]
        public void IdColumn_returns_Id_Property_If_No_KeyAttribute()
        {
            var mm = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(IdPropertyClass));
            var expected = "ID";

            var actual = BaseGridRenderer.IdColumn(mm);

            Assert.AreEqual(expected, actual);
        }

        private class NoKeyClass
        {
            public int Foo { get; set; }
            public int MyKey { get; set; }
            public int Bar { get; set; }
        }

        [TestMethod]
        public void IdColumn_returns_null_if_no_key_found()
        {
            var mm = ModelMetadataProviders.Current.GetMetadataForType(null, typeof(NoKeyClass));

            var actual = BaseGridRenderer.IdColumn(mm);

            Assert.IsNull(actual);
        }
    }
}
